<template>
  <div class="m-list">
    <el-form inline>
      <el-form-item label="姓名">
        <el-input v-model="queryCondition.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="活动区域">
        <el-select v-model="queryCondition.level" placeholder="头衔">
          <el-option label="高级讲师" value="1"></el-option>
          <el-option label="特级讲师" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryCondition.begin"
          type="datetime"
          placeholder="请选择开始时间"
          align="left"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-date-picker
          v-model="queryCondition.end"
          type="datetime"
          placeholder="请选择截至时间"
          align="right"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList()">查询</el-button>
        <el-button type="warning" @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>


    <el-table border :data="list" style="width: 100%">
      <el-table-column label="#" width="70" align="center" type="index">
      </el-table-column>
      <el-table-column
        prop="name" label="姓名" >
      </el-table-column>
      <el-table-column
        prop="career" label="资历">
      </el-table-column>
      <el-table-column label="头衔">
        <template slot-scope="scope">
          {{scope.row.level === 1 ? '高级讲师' : '特级讲师'}}
        </template>
      </el-table-column>
      <el-table-column label="添加时间" prop="gmtCreate">
      </el-table-column>
      <el-table-column label="排序" prop="sort" align="center" header-align="center">
      </el-table-column>
      <el-table-column label="操作" header-align="center">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
          <router-link :to="'/teacher/edit/' + scope.row.id"><el-button type="primary" icon="el-icon-refresh">修改</el-button></router-link>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination :current-page="page"
                   :page-size="limit"
                   :total="total"
                   style="padding: 30px 0; text-align: center"
                   layout="total,prev,pager,next,jumper"
                   @current-change="getList">
    </el-pagination>

  </div>
</template>

<script>
  import teacher from '@/api/edu/teacher'

  export default {
    name: 'list',
    data() {
      return {
        //查询之后接口返回的集合
        list: null,
        page: 1,
        limit: 6,
        total: 0,
        queryCondition: {}
      }
    },
    //页面渲染之前调用，一般调用methods中的定义的方法
    created() {
      this.getList()
    },
    methods: {
      //条件查询讲师的列表
      //传入page表示定义当前页
      getList(page = 1) {
        this.page = page
        teacher.getTeacherListPage(this.page, this.limit, this.queryCondition)
          .then(response => {
            this.list = response.data.data
            this.total = response.data.total
            console.log(response)
          })
          .catch(error => {
            console.log(error)
          })
      },

      //清空表单中的数据并且查询所有的数据
      resetData() {
        this.queryCondition = {}
        this.getList()
      },

      //根据Id删除改行的数据
      removeDataById(id){
        this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //发起删除请求
          teacher.deleteTeacherById(id).then(response => {
            this.getList(this.page);
          });
            this.$message.success("删除成功");
        })
      },


    }
  }
</script>

<style scoped>
.m-list{
  margin-top: 10px;
  margin-left: 20px;
}
</style>
